package com.cykj.page04;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page04
 * @Author: 阿隆
 * @CreateTime: 2025-10-29 19:25
 * @Description: 类对象去操作成员属性(字段)
 * @Version: 1.0
 */
public class Lesson03 {
    public static void main(String[] args) {
        try {
            Class<?> cls = Class.forName("com.cykj.page04.Student");

            Student stu = (Student)cls.newInstance();

            //获取所有的属性 (public)
            Field[] fields = cls.getFields();

            //获取所有的属性 private
            Field[] declaredFields = cls.getDeclaredFields();

            Field field = cls.getField("name");

            //要给属性赋值 , 得确实给那个对象的属性赋值
            field.set(stu , "李四");

            System.out.println(stu.name);


            Field field1 = cls.getDeclaredField("age");

            field1.setAccessible(true);

            field1.set(stu , 30);
            System.out.println(stu.getAge());


            System.out.println(Arrays.toString(declaredFields));

            System.out.println(Arrays.toString(fields));


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}